
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;

public class StreamAdapter : CrossBindingAdaptor
{
    public override Type BaseCLRType
    {
        get
        {
            return typeof(System.IO.Stream);//这是你想继承的那个类
        }
    }
    public override Type AdaptorType
    {
        get
        {
            return typeof(Adaptor);//这是实际的适配器类
        }
    }
    public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
    {
        return new Adaptor(appdomain, instance);//创建一个新的实例
    }
    //实际的适配器类需要继承你想继承的那个类，并且实现CrossBindingAdaptorType接口
    public class Adaptor : System.IO.Stream, CrossBindingAdaptorType
    {
        ILTypeInstance instance;
        ILRuntime.Runtime.Enviorment.AppDomain appdomain;
        //缓存这个数组来避免调用时的GC Alloc
        object[] param1 = new object[1];
        public Adaptor()
        {

        }
        public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
        {
            this.appdomain = appdomain;
            this.instance = instance;
        }
        public ILTypeInstance ILInstance { get { return instance; } }


        bool m_bget_CanReadGot = false;
        IMethod m_get_CanRead = null;
        public override System.Boolean CanRead
        {
            get
            {
                if (!m_bget_CanReadGot)
                {
                    m_get_CanRead = instance.Type.GetMethod("get_CanRead", 0);
                    m_bget_CanReadGot = true;
                }
                if (m_get_CanRead != null)
                {
                    return (System.Boolean)appdomain.Invoke(m_get_CanRead, instance, null);
                }
                else
                {
                    return false;
                }
            }
        }
        bool m_bget_CanSeekGot = false;
        IMethod m_get_CanSeek = null;
        public override System.Boolean CanSeek
        {
            get
            {
                if (!m_bget_CanSeekGot)
                {
                    m_get_CanSeek = instance.Type.GetMethod("get_CanSeek", 0);
                    m_bget_CanSeekGot = true;
                }
                if (m_get_CanSeek != null)
                {
                    return (System.Boolean)appdomain.Invoke(m_get_CanSeek, instance, null);
                }
                else
                {
                    return false;
                }
            }
        }
        bool m_bget_CanTimeoutGot = false;
        IMethod m_get_CanTimeout = null;
        public override System.Boolean CanTimeout
        {
            get
            {
                if (!m_bget_CanTimeoutGot)
                {
                    m_get_CanTimeout = instance.Type.GetMethod("get_CanTimeout", 0);
                    m_bget_CanTimeoutGot = true;
                }
                if (m_get_CanTimeout != null)
                {
                    return (System.Boolean)appdomain.Invoke(m_get_CanTimeout, instance, null);
                }
                else
                {
                    return false;
                }
            }
        }
        bool m_bget_CanWriteGot = false;
        IMethod m_get_CanWrite = null;
        public override System.Boolean CanWrite
        {
            get
            {
                if (!m_bget_CanWriteGot)
                {
                    m_get_CanWrite = instance.Type.GetMethod("get_CanWrite", 0);
                    m_bget_CanWriteGot = true;
                }
                if (m_get_CanWrite != null)
                {
                    return (System.Boolean)appdomain.Invoke(m_get_CanWrite, instance, null);
                }
                else
                {
                    return false;
                }
            }
        }
        bool m_bget_LengthGot = false;
        IMethod m_get_Length = null;
        public override System.Int64 Length
        {
            get
            {
                if (!m_bget_LengthGot)
                {
                    m_get_Length = instance.Type.GetMethod("get_Length", 0);
                    m_bget_LengthGot = true;
                }
                if (m_get_Length != null)
                {
                    return (System.Int64)appdomain.Invoke(m_get_Length, instance, null);
                }
                else
                {
                    return 0;
                }
            }
        }
        bool m_bget_PositionGot = false;
        IMethod m_get_Position = null;
        bool m_bset_PositionGot = false;
        IMethod m_set_Position = null;
        public override System.Int64 Position
        {
            get
            {
                if (!m_bget_PositionGot)
                {
                    m_get_Position = instance.Type.GetMethod("get_Position", 0);
                    m_bget_PositionGot = true;
                }
                if (m_get_Position != null)
                {
                    return (System.Int64)appdomain.Invoke(m_get_Position, instance, null);
                }
                else
                {
                    return 0;
                }
            }
            set {
                if (!m_bset_PositionGot)
                {
                    m_set_Position = instance.Type.GetMethod("set_Position");
                    m_bset_PositionGot = true;
                }
                if (m_set_Position != null)
                {
                    appdomain.Invoke(m_set_Position, instance, value);
                }
                else
                {

                }
            }
        }
       
        bool m_bget_ReadTimeoutGot = false;
        IMethod m_get_ReadTimeout = null;
        public System.Int32 ReadTimeout()
        {
            if (!m_bget_ReadTimeoutGot)
            {
                m_get_ReadTimeout = instance.Type.GetMethod("get_ReadTimeout", 0);
                m_bget_ReadTimeoutGot = true;
            }
            if (m_get_ReadTimeout != null)
            {
                return (System.Int32)appdomain.Invoke(m_get_ReadTimeout, instance, null);
            }
            else
            {
                return 0;
            }
        }
        bool m_bset_ReadTimeoutGot = false;
        IMethod m_set_ReadTimeout = null;
        public void set_ReadTimeout(System.Int32 arg0)
        {
            if (!m_bset_ReadTimeoutGot)
            {
                m_set_ReadTimeout = instance.Type.GetMethod("set_ReadTimeout", 1);
                m_bset_ReadTimeoutGot = true;
            }
            if (m_set_ReadTimeout != null)
            {
                appdomain.Invoke(m_set_ReadTimeout, instance, arg0);
            }
            else
            {

            }
        }
        bool m_bget_WriteTimeoutGot = false;
        IMethod m_get_WriteTimeout = null;
        public System.Int32 WriteTimeout()
        {
            if (!m_bget_WriteTimeoutGot)
            {
                m_get_WriteTimeout = instance.Type.GetMethod("get_WriteTimeout", 0);
                m_bget_WriteTimeoutGot = true;
            }
            if (m_get_WriteTimeout != null)
            {
                return (System.Int32)appdomain.Invoke(m_get_WriteTimeout, instance, null);
            }
            else
            {
                return 0;
            }
        }
        bool m_bset_WriteTimeoutGot = false;
        IMethod m_set_WriteTimeout = null;
        public void set_WriteTimeout(System.Int32 arg0)
        {
            if (!m_bset_WriteTimeoutGot)
            {
                m_set_WriteTimeout = instance.Type.GetMethod("set_WriteTimeout", 1);
                m_bset_WriteTimeoutGot = true;
            }
            if (m_set_WriteTimeout != null)
            {
                appdomain.Invoke(m_set_WriteTimeout, instance, arg0);
            }
            else
            {

            }
        }
        bool m_bCopyToAsyncGot = false;
        IMethod m_CopyToAsync = null;
        public System.Threading.Tasks.Task CopyToAsync(System.IO.Stream arg0, System.Int32 arg1, System.Threading.CancellationToken arg2)
        {
            if (!m_bCopyToAsyncGot)
            {
                m_CopyToAsync = instance.Type.GetMethod("CopyToAsync", 3);
                m_bCopyToAsyncGot = true;
            }
            if (m_CopyToAsync != null)
            {
                return (System.Threading.Tasks.Task)appdomain.Invoke(m_CopyToAsync, instance, arg0, arg1, arg2);
            }
            else
            {
                return null;
            }
        }
        bool m_bCopyToGot = false;
        IMethod m_CopyTo = null;
        public void CopyTo(System.IO.Stream arg0, System.Int32 arg1)
        {
            if (!m_bCopyToGot)
            {
                m_CopyTo = instance.Type.GetMethod("CopyTo", 2);
                m_bCopyToGot = true;
            }
            if (m_CopyTo != null)
            {
                appdomain.Invoke(m_CopyTo, instance, arg0, arg1);
            }
            else
            {

            }
        }
        bool m_bCloseGot = false;
        IMethod m_Close = null;
        public void Close()
        {
            if (!m_bCloseGot)
            {
                m_Close = instance.Type.GetMethod("Close", 0);
                m_bCloseGot = true;
            }
            if (m_Close != null)
            {
                appdomain.Invoke(m_Close, instance, null);
            }
            else
            {

            }
        }
        bool m_bDisposeGot = false;
        IMethod m_Dispose = null;
        public void Dispose()
        {
            if (!m_bDisposeGot)
            {
                m_Dispose = instance.Type.GetMethod("Dispose", 0);
                m_bDisposeGot = true;
            }
            if (m_Dispose != null)
            {
                appdomain.Invoke(m_Dispose, instance, null);
            }
            else
            {

            }
        }
        bool m_bFlushGot = false;
        IMethod m_Flush = null;
        public override void Flush()
        {
            if (!m_bFlushGot)
            {
                m_Flush = instance.Type.GetMethod("Flush", 0);
                m_bFlushGot = true;
            }
            if (m_Flush != null)
            {
                appdomain.Invoke(m_Flush, instance, null);
            }
            else
            {

            }
        }
        bool m_bFlushAsyncGot = false;
        IMethod m_FlushAsync = null;
        public System.Threading.Tasks.Task FlushAsync(System.Threading.CancellationToken arg0)
        {
            if (!m_bFlushAsyncGot)
            {
                m_FlushAsync = instance.Type.GetMethod("FlushAsync", 1);
                m_bFlushAsyncGot = true;
            }
            if (m_FlushAsync != null)
            {
                return (System.Threading.Tasks.Task)appdomain.Invoke(m_FlushAsync, instance, arg0);
            }
            else
            {
                return null;
            }
        }
        bool m_bBeginReadGot = false;
        IMethod m_BeginRead = null;
        public System.IAsyncResult BeginRead(System.Byte[] arg0, System.Int32 arg1, System.Int32 arg2, System.AsyncCallback arg3, System.Object arg4)
        {
            if (!m_bBeginReadGot)
            {
                m_BeginRead = instance.Type.GetMethod("BeginRead", 5);
                m_bBeginReadGot = true;
            }
            if (m_BeginRead != null)
            {
                return (System.IAsyncResult)appdomain.Invoke(m_BeginRead, instance, arg0, arg1, arg2, arg3, arg4);
            }
            else
            {
                return null;
            }
        }
        bool m_bEndReadGot = false;
        IMethod m_EndRead = null;
        public System.Int32 EndRead(System.IAsyncResult arg0)
        {
            if (!m_bEndReadGot)
            {
                m_EndRead = instance.Type.GetMethod("EndRead", 1);
                m_bEndReadGot = true;
            }
            if (m_EndRead != null)
            {
                return (System.Int32)appdomain.Invoke(m_EndRead, instance, arg0);
            }
            else
            {
                return 0;
            }
        }

        bool m_bBeginWriteGot = false;
        IMethod m_BeginWrite = null;
        public System.IAsyncResult BeginWrite(System.Byte[] arg0, System.Int32 arg1, System.Int32 arg2, System.AsyncCallback arg3, System.Object arg4)
        {
            if (!m_bBeginWriteGot)
            {
                m_BeginWrite = instance.Type.GetMethod("BeginWrite", 5);
                m_bBeginWriteGot = true;
            }
            if (m_BeginWrite != null)
            {
                return (System.IAsyncResult)appdomain.Invoke(m_BeginWrite, instance, arg0, arg1, arg2, arg3, arg4);
            }
            else
            {
                return null;
            }
        }
        bool m_bEndWriteGot = false;
        IMethod m_EndWrite = null;
        public void EndWrite(System.IAsyncResult arg0)
        {
            if (!m_bEndWriteGot)
            {
                m_EndWrite = instance.Type.GetMethod("EndWrite", 1);
                m_bEndWriteGot = true;
            }
            if (m_EndWrite != null)
            {
                appdomain.Invoke(m_EndWrite, instance, arg0);
            }
            else
            {

            }
        }
        bool m_bWriteAsyncGot = false;
        IMethod m_WriteAsync = null;
        public System.Threading.Tasks.Task WriteAsync(System.Byte[] arg0, System.Int32 arg1, System.Int32 arg2, System.Threading.CancellationToken arg3)
        {
            if (!m_bWriteAsyncGot)
            {
                m_WriteAsync = instance.Type.GetMethod("WriteAsync", 4);
                m_bWriteAsyncGot = true;
            }
            if (m_WriteAsync != null)
            {
                return (System.Threading.Tasks.Task)appdomain.Invoke(m_WriteAsync, instance, arg0, arg1, arg2, arg3);
            }
            else
            {
                return null;
            }
        }
        bool m_bSeekGot = false;
        IMethod m_Seek = null;
        public override System.Int64 Seek(System.Int64 arg0, System.IO.SeekOrigin arg1)
        {
            if (!m_bSeekGot)
            {
                m_Seek = instance.Type.GetMethod("Seek", 2);
                m_bSeekGot = true;
            }
            if (m_Seek != null)
            {
                return (System.Int64)appdomain.Invoke(m_Seek, instance, arg0, arg1);
            }
            else
            {
                return 0;
            }
        }
        bool m_bSetLengthGot = false;
        IMethod m_SetLength = null;
        public override void SetLength(System.Int64 arg0)
        {
            if (!m_bSetLengthGot)
            {
                m_SetLength = instance.Type.GetMethod("SetLength", 1);
                m_bSetLengthGot = true;
            }
            if (m_SetLength != null)
            {
                appdomain.Invoke(m_SetLength, instance, arg0);
            }
            else
            {

            }
        }
        bool m_bReadGot = false;
        IMethod m_Read = null;
        public override System.Int32 Read(System.Byte[] arg0, System.Int32 arg1, System.Int32 arg2)
        {
            if (!m_bReadGot)
            {
                m_Read = instance.Type.GetMethod("Read", 3);
                m_bReadGot = true;
            }
            if (m_Read != null)
            {
                return (System.Int32)appdomain.Invoke(m_Read, instance, arg0, arg1, arg2);
            }
            else
            {
                return 0;
            }
        }
        bool m_bReadByteGot = false;
        IMethod m_ReadByte = null;
        public System.Int32 ReadByte()
        {
            if (!m_bReadByteGot)
            {
                m_ReadByte = instance.Type.GetMethod("ReadByte", 0);
                m_bReadByteGot = true;
            }
            if (m_ReadByte != null)
            {
                return (System.Int32)appdomain.Invoke(m_ReadByte, instance, null);
            }
            else
            {
                return 0;
            }
        }
        bool m_bWriteGot = false;
        IMethod m_Write = null;
        public override void Write(System.Byte[] arg0, System.Int32 arg1, System.Int32 arg2)
        {
            if (!m_bWriteGot)
            {
                m_Write = instance.Type.GetMethod("Write", 3);
                m_bWriteGot = true;
            }
            if (m_Write != null)
            {
                appdomain.Invoke(m_Write, instance, arg0, arg1, arg2);
            }
            else
            {

            }
        }
        bool m_bWriteByteGot = false;
        IMethod m_WriteByte = null;
        public void WriteByte(System.Byte arg0)
        {
            if (!m_bWriteByteGot)
            {
                m_WriteByte = instance.Type.GetMethod("WriteByte", 1);
                m_bWriteByteGot = true;
            }
            if (m_WriteByte != null)
            {
                appdomain.Invoke(m_WriteByte, instance, arg0);
            }
            else
            {

            }
        }

    }
}